package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess1.entity.ReportHuiXianEntity;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.entity.ReportTemplateEntity;
import com.css.fxfzaqbz.modules.data.queryParams.ReportTemplateEntityQueryParams;
import com.css.fxfzaqbz.modules.data.repository.ReportTemplateRepository;
import com.css.fxfzaqbz.modules.data.vo.CheckReportVO;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-11-04 15:26
 */
@Repository
public class ReportTemplateRepositoryImpl implements ReportTemplateRepository {



    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Object saveReportTemplate(ReportTemplateEntity reportTemplateEntity) {
        String sql = "insert into sys_report_template (" +
                "rt_id,rt_type,rt_pepole,rt_name,att_id,rt_time,del_flag,rt_create_user,rt_create_time)" +
                "values" +
                "(:rtId,:rtType,:rtPepole,:rtName,:attId,:rtTime,:delFlag,:rtCreateUser,:rtCreateTime)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(reportTemplateEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return reportTemplateEntity;
    }

    @Override
    public Map<String, Object> getAllReportTemplate(ReportTemplateEntityQueryParams queryParams) {
        StringBuilder sql = new StringBuilder(" select  u.* from sys_report_template u ");

        StringBuilder orderSql = new StringBuilder(" order by u.rt_time  desc ");

        StringBuilder whereSql = new StringBuilder(" where u.del_flag = '" + YNEnum.N.toString() + "'");

        if (!PlatformObjectUtils.isEmpty(queryParams.getRtType())) {
            whereSql.append(" and u.rt_type = '"+queryParams.getRtType()+"' ");
        }

        if (!PlatformObjectUtils.isEmpty(queryParams.getRtPepole())) {
            whereSql.append(" and u.rt_pepole  like '%" + queryParams.getRtPepole() +"%'");
        }
       /* if (!PlatformObjectUtils.isEmpty(queryParams.getRtStartTime())) {
            whereSql.append(" and u.rt_time >='" + queryParams.getRtStartTime() + "' ");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getRtEndTime())) {
            whereSql.append(" and u.rt_end_time <='" + queryParams.getRtEndTime() + "'");
        }*/

        if (!PlatformObjectUtils.isEmpty(queryParams.getRtStartTime()) && !PlatformObjectUtils.isEmpty(queryParams.getRtEndTime())) {
            whereSql.append(" and u.rt_time between '" + queryParams.getRtStartTime() + "' and '" + queryParams.getRtEndTime() + "'");
        }

        int firstIndex = (queryParams.getCurPage() - 1) * queryParams.getPageSize();
        //int lastIndex=queryParams.getCurPage() * queryParams.getPageSize();
        int lastIndex = queryParams.getPageSize();
        sql.append(whereSql).append(orderSql).toString();
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ImportantActivity.class)).size();
        List<ReportTemplateEntity> reportTemplateEntities = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(ReportTemplateEntity.class));
        System.out.println(sql.toString());
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", reportTemplateEntities);
        return map;

    }

    @Override
    public void delReportTemplate(List<String> ids, String userID, Timestamp currentSqlDate) {
        String sql = "update sys_report_template set del_flag = '" + YNEnum.Y + "' , rt_update_time = '" + currentSqlDate + "' ,rt_update_user = '" + userID + "'" + " where rt_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<ReportTemplateEntity> getList(String rtType) {
        String sql="select * from  sys_report_template where rt_type='"+rtType+"' and del_flag='"+YNEnum.N+"' ";
        List<ReportTemplateEntity> reportTemplateEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ReportTemplateEntity.class));
        return reportTemplateEntities;
    }

    @Override
    public Boolean checkReportTemplate(CheckReportVO checkReportVO) {
        String sql="select * from  sys_report_template where rt_type='"+checkReportVO.getType()+"' and  rt_name ='"+checkReportVO.getName()+"' and del_flag='"+YNEnum.N+"' ";
        try {
            ReportTemplateEntity reportTemplateEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ReportTemplateEntity.class));
            //能查询到  重复 返回 true
            return true;
        } catch (DataAccessException e) {
            return false;
        }
    }

    @Override
    public ReportTemplateEntity getReportTemplateById(String id) {
        String sql="select * from  sys_report_template where rt_id='"+id+"' and del_flag='"+YNEnum.N+"' ";
        ReportTemplateEntity reportTemplateEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ReportTemplateEntity.class));
        return  reportTemplateEntity;
    }

    @Override
    public void updateReportId(String id, String attachId, String attachName) {
        String sql = "update sys_report_template set report_temp_id = '" +attachId+ "' , report_temp_name = '" + attachName + "'   where rt_id ='"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public ReportHuiXianEntity insertReportHuiXian(String iaId, String attachId, String attachName, String number) {
        String uuid = UUIDGenerator.getUUID();
        ReportHuiXianEntity reportHuiXianEntity=new ReportHuiXianEntity(uuid,iaId,attachId,number,attachName, PlatformDateUtils.getCurrentTimestamp());
        String sql = "insert into sys_report_huixian (" +
                "id,ia_id,att_id,number,att_name,create_time)" +
                "values" +
                "(:id,:iaId,:attId,:number,:attName,:createTime)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(reportHuiXianEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return reportHuiXianEntity;
    }

    @Override
    public List<ReportHuiXianEntity> getReportHuiXianByIaIdAndNumber(String iaId, String number) {
        String sql="select * from  sys_report_huixian where ia_id='"+iaId+"' and number='"+number+"' order by create_time desc ";
        List<ReportHuiXianEntity> reportHuiXianEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ReportHuiXianEntity.class));
        return reportHuiXianEntities;
    }

    @Override
    public void updateReportHuiXian(String iaId, String attachId, String attachName, String number) {
        String sql = "update sys_report_huixian set att_id = '" +attachId+ "' , att_name = '" + attachName + "'   where ia_id ='"+iaId+"' and number ='"+number+"'";
        jdbcTemplate.update(sql);
    }

}
